import request from '@/utils/request';
import { Wrapper } from './types/result';

// 上传文件接口
export function uploadFile(data: any, progress?: (e: number) => void) {
  return request<Wrapper<boolean>>({
    url: '/api/attachinfo/upload',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    method: 'POST',
    data,
    transformRequest: [d => {
      const formData = new FormData()
      for (const key in d) {
        formData.append(key, d[key])
      }
      return formData
    }],
    // 上传进度
    onUploadProgress: (e) => {
      const complete = e.loaded / Number(e.total) * 100 | 0
      progress && progress(complete)
    }
  })
}